// pages/component/data-tree/data-tree.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    treeList: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    trees: []
  },

  lifetimes: {
    attached() {
     this.renderTree()
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 生成数据
    renderTree() {
      let list = this.data.treeList;
      let newList = [];
      render(list, 0)
      this.setData({
        trees: newList
      })
      console.log(this.data.trees)
      function render(list, leva) {
        for(let i = 0; i < list.length; i++) {
          let item = list[i];
          item.leva = leva;
          item.checked = false;
          if(item.leva == 0) {
            item.status = false;
            item.show = true;
          } else {
            item.status = false;
            item.show = false;
          }
          newList.push(item);
          if (item.children && item.children.length) {
            render(item.children, leva + 1)
          }
        }
      }
    },

    //显示、隐藏
    changeStatus(e) {
      let {id, status} = e.currentTarget.dataset.item
      let list = this.data.trees;
      let item = {};
      getItem(list,id)
      function getItem(list,id) {
        for(let i = 0; i < list.length; i++) {
          if(list[i].id == id) {
            item = list[i];
            item.status = !item.status;
          } else if(list[i].children && list[i].children.length){
            getItem(list[i].children)
          }
        }
      }
      console.log(item)
      if(item.status) {
        for(let i = 0; i < item.children.length; i++) {
          item.children[i].show = true;
         }
      } else {
        hideTree(item.children);
        function hideTree(list) {
          list.forEach(child => {
            child.show = false;
            child.status = false;
            if(child.children && child.children.length) {
              hideTree(child.children)
            }
          })
        }
      }
    
       this.setData({
         trees: list
       })
    },

    // 选择数据
    selectTree(e) {
      let that = this;
      let {id, checked,pid} = e.currentTarget.dataset.item;
      console.log(id)
      let list = this.data.trees;
      let item = {};
      // 单选
      for(let i = 0; i < list.length; i++) {
        if(list[i].id == id) {
          item = list[i];
          item.checked = !item.checked;
          if(item.pid) {
            console.log(item.pid)
            this.isAll(item.pid)
          }
        }
      }

      //全选
      if(item.children && item.children.length) {
        select(item.children)
      }
     
      function select(list) {
        for(let i = 0; i <list.length; i++) {
         list[i].checked = item.checked;
        
         if(list[i].children && list[i].children.length) {
          select(list[i].children)
         }
        }
      }

      this.setData({
        trees: list
      })
    },

    // 判断子节点时候全部选中
    isAll(pid,id) {
      let pItem = {};
      let all = true;
      let list = this.data.trees;
      
      checked(pid)
      this.setData({
        trees: list
      })
      function checked(pid) {
        list.forEach(node => {
          if(node.id == pid) {
            pItem = node;
            pItem.children.forEach(node2 => {
              if(!node2.checked) {
                all = false;
                return;
              }
            })
            pItem.checked = all;
            if(pItem.pid) {
              checked(pItem.pid)
            }
          }
        })
      }
    },

    // 获取选中节点
    getSelected() {
      let list = this.data.trees;
      let newList = [];
      for(let i = 0; i < list.length; i++) {
        if(list[i].checked) {
          newList.push(list[i])
        }
      }

      return newList;
    }

  }
})